package com.fire.aop.cglib;

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

/**
 * @author zwd
 * @date 2021/8/15
 * @email zwd@hhh.com
 */
public class CglibMain {
	public static void main(String ...args) {
		Enhancer enhancer = new Enhancer();
		// 设置被代理的类对象
		enhancer.setSuperclass(Foo.class);
		enhancer.setInterfaces(new Class[] {FooInterface.class});
		enhancer.setCallback(new CallbackImpl());
		Foo fooProxy = (Foo) enhancer.create();
		fooProxy.setName("name");
		System.out.println(fooProxy.getName());

		fooProxy.print();
	}

	private static class CallbackImpl implements MethodInterceptor {
		@Override
		public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
			System.out.println("intercept: " + method.getName());
			return proxy.invokeSuper(obj, args);
		}
	}
}
